package com.persondemo.videoappliction.ui.search.presenter;

import android.text.TextUtils;

import com.persondemo.videoappliction.bean.VipSeachBean;
import com.persondemo.videoappliction.net.BaseApi;
import com.persondemo.videoappliction.net.BaseObserver;
import com.persondemo.videoappliction.net.RxSchedulers;
import com.persondemo.videoappliction.ui.base.BasePresenter;
import com.persondemo.videoappliction.ui.search.contract.SeachVipContract;

import javax.inject.Inject;

/**
 * Created by Administrator on 2018/4/16.
 */

public class SeachVipPresenter extends BasePresenter<SeachVipContract.View> implements SeachVipContract.presenter {
    BaseApi baseApi;

    @Inject
    public SeachVipPresenter(BaseApi baseApi) {
        this.baseApi = baseApi;
    }

    @Override
    public void getseachdata(String key, int page) {
        baseApi.seachvip(key, page)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new BaseObserver<VipSeachBean>() {
                    @Override
                    public void onSuccess(VipSeachBean vipSeachBean) {
                        VipSeachBean.DataBean dataBean = new VipSeachBean.DataBean();
                        if (vipSeachBean != null && vipSeachBean.getStatus() == 1) {
                            for (int i = 0; i < vipSeachBean.getData().size(); i++) {
                                dataBean = vipSeachBean.getData().get(i);
                                if (TextUtils.isEmpty(vipSeachBean.getData().get(i).getImgs())) {
                                    dataBean.itemType = VipSeachBean.DataBean.NO_IMG;
                                } else {
                                    dataBean.itemType = VipSeachBean.DataBean.HAVE_IMG;
                                }
                            }
                        }
                        if (page > 1) {
                            mView.getmoredata(vipSeachBean);
                        } else {
                            mView.loaddata(vipSeachBean);
                        }
                    }

                    @Override
                    public void onFail(Throwable e) {
                        mView.showFaild();
                    }
                });
    }
}
